use diesel::r2d2::{ConnectionManager, Pool, PoolError };
use diesel::sqlite::SqliteConnection;
use dotenv::dotenv;

pub type SqlitePool = Pool<ConnectionManager<SqliteConnection>>;

fn init_pool(database_url: &str) -> Result<SqlitePool, PoolError> {
  let manager = ConnectionManager::<SqliteConnection>::new(database_url);
  Pool::builder().build(manager)
}

pub fn establish_connection_pool( sql_file_path:&str) -> SqlitePool {
  dotenv().ok();

  let database_url = sql_file_path.to_owned(); //env::var("DATABASE_URL").expect("DATABASE_URL must be set and inited, refer to common::init::init()");
  init_pool(&database_url).expect("Failed to create pool")
}
